
package com.ifcar.app.activity;

import com.ifcar.app.fragment.FragmentUtil;
import com.ifcar.app.fragment.NRFragment;
import com.ifcar.app.fragment.NRTabFragment;
import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.activity.R;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 通用activity
 * 
 * @author wjying
 */
public class CommonFrameActivity extends BaseActivity {
    static final String COMMON_FRAME_START_ME_PARAM = "COMMON_FRAME_START_ME_PARAM";

    static final String FRAGMENT_NAME = "fragment_name";

    static final String FRAGMENT_TAG = "fragment_tag";

    static final String FRAGMENT_ARGU = "fragment_argu";

    static final String TITLE = "title";

    static final String EXTRA = "extra";

    public static final String EXTRA_PARAM_LAYOUT = "layout";

    public static final String EXTRA_PARAM_CONTAINER = "container";

    public static final String EXTRA_PARAM_WINDOW_FRATURE = "window_feature";

    public static final String EXTRA_PARAM_WINDOW_FLAGS = "window_flags";

    View mTitleBarView;

    NRFragment mFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Bundle param = getIntent().getBundleExtra(COMMON_FRAME_START_ME_PARAM);
        Bundle extraParam = param.getBundle(EXTRA);

        int windowFeature = extraParam == null ? Window.FEATURE_NO_TITLE : extraParam.getInt(
                EXTRA_PARAM_WINDOW_FRATURE, Window.FEATURE_NO_TITLE);
        requestWindowFeature(windowFeature);

        int[] flags = extraParam == null ? null : extraParam.getIntArray(EXTRA_PARAM_WINDOW_FLAGS);
        if (flags != null) {
            Window window = getWindow();
            for (int flag : flags) {
                window.setFlags(flag, flag);
            }
        }

        super.onCreate(savedInstanceState);

        int layoutid = extraParam == null ? R.layout.common_frame_layout : extraParam.getInt(
                EXTRA_PARAM_LAYOUT, R.layout.common_frame_layout);
        if (layoutid != 0) {
            setContentView(layoutid);
        }

        mTitleBarView = findViewById(R.id.top_main_bottom);
        
        if (mTitleBarView != null) {
            mTitleBarView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mFragment != null) {
                        mFragment.dispatchActivityViewClick(v);
                    }
                }
            });
        }
        
    }
    
    private void removeFragment() {
        if (mFragment != null) {
            if (mFragment instanceof NRTabFragment) {
                ((NRTabFragment) mFragment).removeAllTabFragment();
            }

            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.remove(mFragment);
            FragmentUtil.commitAndExec(fm, ft, false);
            mFragment = null;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Bundle recreateArgs = mFragment == null ? null : mFragment.needRecreate();
        if (recreateArgs != null) {
            removeFragment();
            Bundle param = getIntent().getBundleExtra(COMMON_FRAME_START_ME_PARAM);
            param.putBundle(FRAGMENT_ARGU, recreateArgs);
        }
        if (mFragment == null) {
            initFragment();
        }
    }

    private void initFragment() {
        if (mFragment == null) {
            Bundle param = getIntent().getBundleExtra(COMMON_FRAME_START_ME_PARAM);
            Bundle extraParam = param.getBundle(EXTRA);

            String fragmentName = param.getString(FRAGMENT_NAME);
            String fragmentTag = param.getString(FRAGMENT_TAG);
            Bundle argu = param.getBundle(FRAGMENT_ARGU);

            int layoutid = extraParam == null ? R.layout.common_frame_layout : extraParam.getInt(
                    EXTRA_PARAM_LAYOUT, R.layout.common_frame_layout);

            if (layoutid == R.layout.common_frame_layout) {
                mFragment = (NRFragment) addFragmentByTag(R.id.common_frame_content, fragmentName,
                        fragmentTag, argu);
            } else if (layoutid != 0) {
                int containerId = extraParam == null ? 0 : extraParam.getInt(EXTRA_PARAM_CONTAINER,
                        0);
                mFragment = (NRFragment) addFragmentByTag(containerId, fragmentName, fragmentTag,
                        argu);
            } else {
                mFragment = (NRFragment) addFragmentByTag(0, fragmentName, fragmentTag, argu);
                if (mFragment.getView() != null) {
                    setContentView(mFragment.getView(), new FrameLayout.LayoutParams(
                            FrameLayout.LayoutParams.FILL_PARENT,
                            FrameLayout.LayoutParams.FILL_PARENT));
                }
            }

            if (mFragment instanceof NRTabFragment) {
                ((NRTabFragment) mFragment).setup();
            }

            String title = param.getString(TITLE);
            if (!TextUtils.isEmpty(title) && mTitleBarView != null) {
                TextView view = (TextView) mTitleBarView.findViewById(R.id.top_main_text);
                view.setText(title);
            }
            mFragment.selectInTab();
            
            if (!isThemeChanged()) {
                mFragment.applyTheme();
            }
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (mFragment != null && mFragment.dispatchKeyEvent(event)) {
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

    public void showGoBack(boolean show) {
        if (mTitleBarView == null) {
            return;
        }
        View view = mTitleBarView.findViewById(R.id.go_back_common_btn);
        if (view == null) {
            return;
        }
        if (show) {
            view.setVisibility(View.VISIBLE);
        } else {
            view.setVisibility(View.GONE);
        }
    }

    public void goBackClick(View view) {
        finish();
    }

    @Override
    public void onApplyTheme() {
        super.onApplyTheme();
        ThemeSettingsHelper themeSettingsHelper = getThemeSettingsHelper();
        View content = findViewById(R.id.common_frame);
        if (content != null) {
            themeSettingsHelper.setViewBackgroudColor(this, content, R.color.main_bg_color);
        }

        if (mTitleBarView != null) {
            applyThemeForDefaultTitleView(this, themeSettingsHelper, mTitleBarView);
        }

        if (mFragment != null) {
            mFragment.applyTheme();
        }
    }

    public static void applyThemeForDefaultTitleView(Context context,
            ThemeSettingsHelper themeSettingsHelper, View titleView) {
        if (titleView == null) {
            return;
        }

        TextView title = (TextView) titleView.findViewById(R.id.top_main_text);
        if (title != null) {
            themeSettingsHelper.setTextViewColor(context, title, R.color.main_title_color);
        }

        ImageView image = (ImageView) titleView.findViewById(R.id.top_id_image);
        if (image != null) {
            themeSettingsHelper.setImageViewSrc(context, image, R.drawable.netease_top);
        }

        themeSettingsHelper.setViewBackgroud(context, titleView, R.drawable.top_background);

        ImageView goback = (ImageView) titleView.findViewById(R.id.go_back_common_btn);
        if (image != null) {
            themeSettingsHelper.setImageViewSrc(context, goback, R.drawable.go_back);
        }
    }

    public static Intent getStartIntent(Context context, Class clazz, String fragmentTag,
            Bundle fragmentArgu) {
        return getStartIntent(context, clazz.getName(), fragmentTag, fragmentArgu);
    }

    public static Intent getStartIntent(Context context, String fragmentName, String fragmentTag,
            Bundle fragmentArgu) {
        return getStartIntent(context, null, fragmentName, fragmentTag, fragmentArgu);
    }

    public static Intent getStartIntent(Context context, String title, Class clazz,
            String fragmentTag, Bundle fragmentArgu) {
        return getStartIntent(context, title, clazz.getName(), fragmentTag, fragmentArgu);
    }

    public static Intent getStartIntent(Context context, String title, String fragmentName,
            String fragmentTag, Bundle fragmentArgu) {
        return getStartIntent(context, title, fragmentName, fragmentTag, fragmentArgu, null);

    }

    public static Intent getStartIntent(Context context, String title, String fragmentName,
            String fragmentTag, Bundle fragmentArgu, Bundle extra) {
        return getStartIntent(context, title, fragmentName, fragmentTag, fragmentArgu, extra,
                CommonFrameActivity.class);

    }

    public static Intent getStartIntent(Context context, String title, String fragmentName,
            String fragmentTag, Bundle fragmentArgu, Bundle extra, Class clazz) {

        Bundle b = new Bundle();
        b.putString(TITLE, title);
        b.putString(FRAGMENT_NAME, fragmentName);
        b.putString(FRAGMENT_TAG, fragmentTag);
        b.putBundle(FRAGMENT_ARGU, fragmentArgu);
        b.putBundle(EXTRA, extra);
        Intent intent = new Intent(context, clazz);
        intent.putExtra(COMMON_FRAME_START_ME_PARAM, b);
        return intent;

    }
}
